# Name:          script_operatorlevels.pl
# Version:       0.4
# Author:        Jack McCracken
# Date:          2009-01-17
# Description:   Change the color of usernames in channel text based on Operator Levels. Display  status
#                (op, voice, etc)

use strict;
use warnings;
use Xchat qw( :all );

my $NAME    = 'Operator Levels';
my $VERSION = '0.4';

#############################################
#               CONFIGURATION               #
#############################################

my $symbdisp = 0; # Display operand symbol. 0 is off, 1 is on

# The following list of events will have the user name coloroized. Place a '#' before the
# line in order to not have that event have the color replaced
my @events = (
	'Channel Message',
	'Channel Action',
	'Channel Msg Hilight',
	'Channel Action Hilight',
	'Your Message',
	'Your Action',
);

# The following list of events determine the color of the nick. Use Preferences -> Colors
# for available color codes. The list below approximates the colors of the icons in the
# user list. The last line is commented, but is available if you wish to provide a color
# for users with no nick.
my %modes = (
	'+' => 27,
	'%' => 28,
	'@' => 29,
	'&' => 25,
	'~' => 31,
	'' => 26, # If user has no mode
);

#############################################

my %oplevel = (
	'+' => '+',
	'%' => '%',
	'@' => '@',
	'&' => '&',
	'~' => '~',
        '' => '',
);

register($NAME, $VERSION, "Change the color of usernames in channels based on channels status (op, voice, etc)");

for my $event (@events) {
	hook_print($event, \&color_message, { data => $event, priority => PRI_HIGH });
}

my $exit;


Xchat::hook_command("oplev_off",		"oplev_off");
Xchat::hook_command("oplev_on",		        "oplev_on");

sub oplev_off {
	#Turns off operator symbol displaying
	$symbdisp = 0;
	Xchat::print("Will no longer display Operator Symbols.");
	return 1;
}

sub oplev_on {
	#Turns on operator symbol displaying
	$symbdisp = 1;
	Xchat::print("Will now display Operator Symbols.");
	return 1;
}

sub color_message {
	$exit = 0;

	my @msgdata = @{$_[0]};
	my $event = $_[1];

	my $color = $modes{($msgdata[2] || '')};
	my $symbol = $oplevel{($msgdata[2] || '')};
	my $nick = $msgdata[0];

	if ($color) {
		if ($nick =~ /^\003$color/) {
			$exit = 1;
			return EAT_NONE;
		}

		$nick =~ s/\003\d{0,2}(?:,\d{1,2})?// ; # strip color from the nick
		if ($symbdisp) { $nick = "\003$color\002$symbol\002$nick\003";}
                  else {$nick = "\003$color$nick\003";}

		$msgdata[0] = $nick;

		emit_print($event, @msgdata) unless $exit;
		return EAT_ALL;
	}
	return EAT_ALL if $exit;
	return EAT_NONE;
}

__END__
